using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DonutSpawner : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private int nuberOfDonuts = 100;
        private ICamera camera;
        private List<Donut> donuts;

        public DonutSpawner(Game game)
            : base(game)
        {
            donuts = new List<Donut>();
        }

        public override void Initialize()
        {
            Random random = new Random();
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            for (int i = 0; i < nuberOfDonuts; i++)
            {
                donuts.Add(new Donut());
                donuts[i].LoadContent(this.Game.Content);
                donuts[i].Position = new Vector3(random.Next() % 75, random.Next() % 15, random.Next() % 75);
            }

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }


        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            foreach (Donut d in donuts)
            {
                if (!d.recieved)
                {
                    foreach (ModelMesh mesh in d.Model.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.View = camera.ViewMatrix;
                            effect.Projection = camera.ProjectionMatrix;
                            effect.World = Matrix.CreateTranslation(d.Position);
                        }
                        mesh.Draw();
                    }
                    d.UpdateBoundingSphere();
                    base.Draw(gameTime);
                }
                else
                {
                    donuts.Remove(d);
                }
            }
        }
    }
}
